function [debt, vf_error] = debt_value(oParams,setts,f,c,lambda,equity,debt_in,bin_prob,recovery_value)
% Calculates the debt value function using value function iteration

% Variables:
% oParams,setts		see readme
% f			rate of creative destruction
% c			coupon
% lambda	R&D intensity
% equity	equity value
% debt_in	initial guess debt value
% bin_prob	transition probabilities
% recovery_value    recovery_value

% Set parameters
	d_prec = setts.d_prec;

% Starting values
	debt = debt_in;
	vf_error = d_prec + 1;

% Value function iteration
	while vf_error > d_prec

% Update debt value and calculate error
		debt_new = debt_update(oParams,f,c,lambda,equity,debt,bin_prob,recovery_value);
        vf_error = sum(abs((debt - debt_new)));
		debt = debt_new;

	end

end
